﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using System.Collections.ObjectModel;
using CommunitySystem.EmpbasicService;

namespace CommunitySystem.PageView.公务员选拔
{
    public partial class OrgSelectWindow : ChildWindow
    {
        public ObservableCollection<ExameOrg> _orgs = null;
        private EmpbasicService.EmpBasicServiceClient client = null;
        public OrgSelectWindow(ObservableCollection<ExameOrg> orgs)
        {
            InitializeComponent();
            if (orgs == null)
                orgs = new ObservableCollection<ExameOrg>();
            _orgs = orgs;
            client = new EmpbasicService.EmpBasicServiceClient();
            client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(client_GetOrgForHRModelCompleted);
            client.GetOrgForHRModelAsync(0);
        }

        void client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            var root = e.Result.Where(c => c.ParentID == 0).FirstOrDefault();
            TreeViewItem rootItem = new TreeViewItem() { DataContext = root };
            CheckBox cb = new CheckBox() { Content = root.Name, DataContext = root, Tag = rootItem };
            cb.Click += new RoutedEventHandler(cb_Click);
            rootItem.Header = cb;
            if (_orgs != null)
            {
                int count = _orgs.Where(c => c.OrgForHRID == c.ID).Count();
                if (count > 0)
                {
                    cb.IsChecked = true;
                    rootItem.IsExpanded = true;
                }
            }
            AddTreeViewItem(rootItem, root, e.Result);
            orgTree.Items.Add(rootItem);
        }

        void cb_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            OrgForHRModel model = cb.DataContext as OrgForHRModel;
            var o = _orgs.Where(c => c.OrgForHRID == model.ID).FirstOrDefault();
            if (cb.IsChecked == true)
            {
                ExameOrg _o = new ExameOrg() { OrgForHRID = (int)model.ID };
                _orgs.Add(_o);
            }
            else
            {
                _orgs.Remove(o);
            }

            if (cb.Tag != null)
            {
                TreeViewItem tvi = cb.Tag as TreeViewItem;
                foreach (TreeViewItem i in tvi.Items)
                {
                    CheckBox head = i.Header as CheckBox;
                    if (head != null)
                    {
                        head.IsChecked = cb.IsChecked;
                        cb_Click(head, null);
                    }
                }
            }
            
        }

        void AddTreeViewItem(TreeViewItem PItem, OrgForHRModel PModel, ObservableCollection<OrgForHRModel> source)
        {
            foreach (var c in source.Where(c => c.ParentID == PModel.ID))
            {
                TreeViewItem item = new TreeViewItem() { DataContext = c };
                CheckBox cb = new CheckBox() { Content = c.Name, DataContext = c, Tag = item };
                cb.Click += new RoutedEventHandler(cb_Click);
                item.Header = cb;
                if (_orgs != null)
                {
                    int count = _orgs.Where(d => d.OrgForHRID == c.ID).Count();
                    if (count > 0)
                    {
                        cb.IsChecked = true;
                        item.IsExpanded = true;
                    }
                } 
                PItem.Items.Add(item);
                AddTreeViewItem(item, c, source);

            }
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

